package com.dabaozhan.modules.wx_app.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.dabaozhan.common.exception.RRException;
import com.dabaozhan.common.utils.ValidationBeanUtils;
import com.dabaozhan.modules.wx_app.base.PageResult;
import com.dabaozhan.modules.wx_app.base.ResponseDto;
import com.dabaozhan.modules.wx_app.entity.QueryListBaseEntity;
import com.dabaozhan.modules.wx_app.entity.RiskEntity;
import com.dabaozhan.modules.wx_app.enums.ResponseCodeEnum;
import com.dabaozhan.modules.wx_app.facade.RiskFacade;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "风险设置")
@RestController
@RequestMapping("/wx_app")
public class RiskController {
	private static final Logger LOGGER = LoggerFactory.getLogger(RiskController.class);
	
	@Autowired
	private RiskFacade riskFacade;

	@ApiOperation(value = "查询风险设置")
    @RequestMapping(value = "/getRisk")
	public String getRisk(QueryListBaseEntity entity){
		ResponseDto<List<RiskEntity>> responseDto = new ResponseDto<List<RiskEntity>>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			PageResult<List<RiskEntity>> pageResult = riskFacade.getRiskList(entity);
			List<RiskEntity> list = pageResult.getListData();
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			responseDto.setResult(list);
			responseDto.setTotal(pageResult.getTotal());
			responseDto.setPage(entity.getPage());
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("查询风险设置发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("查询风险设置请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "添加风险设置")
    @RequestMapping(value = "/addRisk")
	public String addRisk(RiskEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			//验证参数是否为空
			ValidationBeanUtils.validateBeanRequestDto(entity);
			riskFacade.addRisk(entity);
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("风险设置发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("风险设置请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
	
	@ApiOperation(value = "删除风险设置")
    @RequestMapping(value = "/deleteRisk")
	public String deleteRisk(RiskEntity entity){
		ResponseDto<String> responseDto = new ResponseDto<String>();
		try{
			if(StringUtils.isBlank(entity.getUserId()) || StringUtils.isBlank(entity.getParentId()) || StringUtils.isBlank(entity.getCategoryId())){
				responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_101);
			}else{
				riskFacade.deleteRisk(entity);
				responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_100);
			}
		}catch(RRException re){
			responseDto.setCode(re.getCode());
			responseDto.setMessage(re.getMessage());
		}catch (Exception e) {
			responseDto.setResponseCodeEnum(ResponseCodeEnum.ResponseCode_500);
			//记录异常日志
			LOGGER.error("删除风险设置发生异常,异常详情如下:",e);
		}finally{
			//记录请求入参和返回日志
			LOGGER.info("删除风险设置请求参数:{}-返回结果:{}",JSON.toJSONString(entity),JSON.toJSONString(responseDto));
		}
		return JSON.toJSONString(responseDto);
	}
}
